package com.demo.session;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/session")
public class SessionDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("request.....");
        /**
         * Session对象由服务器管理（Tomcat管理）
         * 我们在程序中只能通过request对象去获取session，不能自己创建Session
         */
        HttpSession session = req.getSession();
        // 设置过期时间5秒
        //session.setMaxInactiveInterval(5);
        // 给session中存储数据
        session.setAttribute("username","张三");
        session.setAttribute("age",20);
        session.setAttribute("address","上海");
        resp.getWriter().write("session...");
    }
}
